/**
 * Copyright 2013-2022 the original author or authors from the Jeddict project (https://jeddict.github.io/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package io.github.jeddict.relation.mapper.properties.index;

import static io.github.jeddict.util.StringUtils.EMPTY;
import io.github.jeddict.relation.mapper.widget.table.TableWidget;
import io.github.jeddict.relation.mapper.properties.tablemember.TableMemberPanel;
import io.github.jeddict.relation.mapper.spec.DBTable;
import io.github.jeddict.jpa.spec.Index;
import org.netbeans.modeler.properties.EntityComponent;
import org.netbeans.modeler.properties.spec.Entity;
import org.netbeans.modeler.properties.spec.RowValue;

public class IndexPanel extends EntityComponent<Index> {

    private Index index;
    private final TableWidget<? extends DBTable> tableWidget;

    public IndexPanel(TableWidget<? extends DBTable> tableWidget) {
        this.tableWidget = tableWidget;
    }

    @Override
    public void postConstruct() {
        initComponents();
    }

    @Override
    public void init() {
        ((TableMemberPanel) tableMemberPanel).init();
        ((TableMemberPanel) tableMemberPanel).setChildFactory(new IndexChildFactory());
        pack();
    }
    

    @Override
    public void createEntity(Class<? extends Entity> entityWrapperType) {
        this.setTitle("Create Index");
        if (entityWrapperType == RowValue.class) {
            this.setEntity(new RowValue(new Object[3]));
        }
        index = new Index();
        ((TableMemberPanel)tableMemberPanel).setTableWidget(tableWidget);
        ((TableMemberPanel) tableMemberPanel).setValue(index.getColumnList());
        nameTextField.setText(EMPTY);
        uniqueCheckBox.setSelected(false);
    }

    @Override
    public void updateEntity(Entity<Index> entityValue) {
        this.setTitle("Update Index");
        if (entityValue.getClass() == RowValue.class) {
            this.setEntity(entityValue);
            Object[] row = ((RowValue) entityValue).getRow();
            index = (Index) row[0];
            ((TableMemberPanel)tableMemberPanel).setTableWidget(tableWidget);
            ((TableMemberPanel) tableMemberPanel).setValue(index.getColumnList());
            nameTextField.setText(index.getName());
            if(index.isUnique()!=null && index.isUnique()){
               uniqueCheckBox.setSelected(true); 
            } else {
                uniqueCheckBox.setSelected(false);
            }
        }
        
    }

    /**
     * This method is called from within the uniqueConstraint to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        rootLayeredPane = new javax.swing.JLayeredPane();
        tableMemberPanel = new io.github.jeddict.relation.mapper.properties.tablemember.TableMemberPanel("Indexes");
        configPane = new javax.swing.JLayeredPane();
        action_jLayeredPane = new javax.swing.JLayeredPane();
        save_Button = new javax.swing.JButton();
        cancel_Button = new javax.swing.JButton();
        nameTextField = new javax.swing.JTextField();
        nameLabel = new javax.swing.JLabel();
        uniqueCheckBox = new javax.swing.JCheckBox();

        rootLayeredPane.setLayout(new java.awt.BorderLayout());

        javax.swing.GroupLayout tableMemberPanelLayout = new javax.swing.GroupLayout(tableMemberPanel);
        tableMemberPanel.setLayout(tableMemberPanelLayout);
        tableMemberPanelLayout.setHorizontalGroup(
            tableMemberPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        tableMemberPanelLayout.setVerticalGroup(
            tableMemberPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 414, Short.MAX_VALUE)
        );

        rootLayeredPane.add(tableMemberPanel, java.awt.BorderLayout.CENTER);

        org.openide.awt.Mnemonics.setLocalizedText(save_Button, org.openide.util.NbBundle.getMessage(IndexPanel.class, "IndexPanel.save_Button.text")); // NOI18N
        save_Button.setToolTipText(org.openide.util.NbBundle.getMessage(IndexPanel.class, "IndexPanel.save_Button.toolTipText")); // NOI18N
        save_Button.setPreferredSize(new java.awt.Dimension(57, 29));
        save_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                save_ButtonActionPerformed(evt);
            }
        });
        action_jLayeredPane.add(save_Button);
        save_Button.setBounds(0, 0, 70, 30);

        org.openide.awt.Mnemonics.setLocalizedText(cancel_Button, org.openide.util.NbBundle.getMessage(IndexPanel.class, "IndexPanel.cancel_Button.text")); // NOI18N
        cancel_Button.setToolTipText(org.openide.util.NbBundle.getMessage(IndexPanel.class, "IndexPanel.cancel_Button.toolTipText")); // NOI18N
        cancel_Button.setPreferredSize(new java.awt.Dimension(60, 23));
        cancel_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancel_ButtonActionPerformed(evt);
            }
        });
        action_jLayeredPane.add(cancel_Button);
        cancel_Button.setBounds(80, 0, 70, 30);

        nameTextField.setToolTipText(org.openide.util.NbBundle.getMessage(IndexPanel.class, "IndexPanel.nameTextField.toolTipText")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(nameLabel, org.openide.util.NbBundle.getMessage(IndexPanel.class, "IndexPanel.name")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(uniqueCheckBox, org.openide.util.NbBundle.getMessage(IndexPanel.class, "IndexPanel.uniqueCheckBox.text")); // NOI18N

        configPane.setLayer(action_jLayeredPane, javax.swing.JLayeredPane.DEFAULT_LAYER);
        configPane.setLayer(nameTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);
        configPane.setLayer(nameLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        configPane.setLayer(uniqueCheckBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout configPaneLayout = new javax.swing.GroupLayout(configPane);
        configPane.setLayout(configPaneLayout);
        configPaneLayout.setHorizontalGroup(
            configPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(configPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(nameLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(configPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(configPaneLayout.createSequentialGroup()
                        .addComponent(uniqueCheckBox, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 113, Short.MAX_VALUE)
                        .addComponent(action_jLayeredPane, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(configPaneLayout.createSequentialGroup()
                        .addComponent(nameTextField)
                        .addContainerGap())))
        );
        configPaneLayout.setVerticalGroup(
            configPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, configPaneLayout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addGroup(configPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(configPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(uniqueCheckBox, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(action_jLayeredPane, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        rootLayeredPane.add(configPane, java.awt.BorderLayout.SOUTH);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(rootLayeredPane, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(rootLayeredPane, javax.swing.GroupLayout.Alignment.TRAILING)
        );
    }// </editor-fold>//GEN-END:initComponents

      private boolean validateField() {
//        if (uniqueConstraints.contains(new Index(nameTextField.getText()))){
//            JOptionPane.showMessageDialog(this, "Index with same name already exist : " + uniqueConstraint.getName(), "Duplicate Index", javax.swing.JOptionPane.WARNING_MESSAGE);
//            return false;
//        }
        return true;
    }
      
    private void save_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_save_ButtonActionPerformed
        if (!validateField()) {
            return;
        }
        index.setColumnList(((TableMemberPanel) tableMemberPanel).getValue().getColumns());
        index.setName(nameTextField.getText());
        index.setUnique(uniqueCheckBox.isSelected());

        if (this.getEntity().getClass() == RowValue.class) {
            Object[] row = ((RowValue) this.getEntity()).getRow();
            row[0] = index;
            row[1] = index.getName();
            row[2] = index.toString();
        }
        saveActionPerformed(evt);
    }//GEN-LAST:event_save_ButtonActionPerformed

    private void cancel_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancel_ButtonActionPerformed
        cancelActionPerformed(evt);
    }//GEN-LAST:event_cancel_ButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLayeredPane action_jLayeredPane;
    private javax.swing.JButton cancel_Button;
    private javax.swing.JLayeredPane configPane;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JLayeredPane rootLayeredPane;
    private javax.swing.JButton save_Button;
    private javax.swing.JPanel tableMemberPanel;
    private javax.swing.JCheckBox uniqueCheckBox;
    // End of variables declaration//GEN-END:variables

}
